﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using InvoiceInvoker.Logic;

namespace InvoiceInvoker.MvcUI.Filters
{
	public class ProductsFilter
	{
		[DisplayName("Nazwa")]
		public string Name { get; set; }

		[DisplayName("Min. cena netto")]
		[RegularExpression("^[0-9]+(,[0-9])?[0-9]*$")]
		public string MinPrice { get; set; }

		[DisplayName("Maks. cena netto")]
		[RegularExpression("^[0-9]+(,[0-9])?[0-9]*$")]
		public string MaxPrice { get; set; }

		public Func<RegisteredProduct, bool> GetExpression()
		{
			string name = Name ?? "";
			decimal minPrice;
			decimal maxPrice;

			decimal.TryParse(MinPrice, out minPrice);

			if (decimal.TryParse(MaxPrice, out maxPrice) == false)
				maxPrice = decimal.MaxValue;

			return product => product.Name.ToLower().StartsWith(name.ToLower()) && product.NetPrice >= minPrice && product.NetPrice <= maxPrice;
		}
	}
}